package com.putao.tinytime.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;

import com.google.gson.Gson;
import com.putao.tinytime.application.GlobalApplication;
import com.putao.tinytime.bean.CollageConfigInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CollageInfoManager {

    private static CollageInfoManager mInstance;

    private CollageConfigInfo mCollageConfigInfo;

    public CollageInfoManager() {
        init();
    }

    void init() {
        mCollageConfigInfo = getCollageConfigInfoFromPreferences(GlobalApplication.getInstance());
    }

    public CollageConfigInfo getCollageConfigInfo() {
        return mCollageConfigInfo;
    }

    // 文件夹名称必须和解压包解压出来的名称保持一致。
    public String getCollageFilePath() {
        return FileOperationHelper.getExternalFilePath()
                + PuTaoConstants.COLLAGE_RESOURCE_PATH;
    }

    public static CollageInfoManager getInstance() {
        if (mInstance == null) {
            mInstance = new CollageInfoManager();
        }
        return mInstance;
    }

    public Bitmap getSampleResourceImage(String name, float scale) {
        Bitmap bitmap = BitmapHelper.getInstance().getBitmapFromPath(getCollageFilePath()
                + name);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    }

//    public CollageConfigInfo getCollageConfigInfoFromExternalFile() {
//        try {
//            String config_str = FileOperationHelper.readJsonFile(
//                    PuTaoConstants.COLLAGE_FLODER_NAME,
//                    PuTaoConstants.COLLAGE_CONFIG_NAME);
//            Gson gson = new Gson();
//            CollageConfigInfo mCollageConfigInfo = gson.fromJson(config_str,
//                    CollageConfigInfo.class);
//            return mCollageConfigInfo;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public CollageConfigInfo getCollageConfigInfoFromPreferences(
            Context context) {
        String config_str = getConfigStringFromPreference(context,
                PuTaoConstants.PREFERENC_COLLAGE_CONFIG_JSON);
        CollageConfigInfo mCollageConfigInfo = null;

        try {
            Gson gson = new Gson();
            mCollageConfigInfo = gson.fromJson(config_str,
                    CollageConfigInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mCollageConfigInfo;
    }

    private String getConfigStringFromPreference(Context context,
                                                 String PreferenceKey) {
        String config_str = SharedPreferencesHelper.readStringValue(context,
                PreferenceKey);
        if (!StringHelper.isEmpty(config_str)) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(config_str);
            config_str = m.replaceAll("");
        }
        return config_str;
    }


}
